package addressbook2;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import addressbook2.View; 
import addressbook2.NewFrame; 
import java.util.*;

//The Controller coordinates interactions between the View and Model

public class Controller {
	private View objectV;
	private Model objectM;
	private String name;
	
		public Controller(View objectV, Model objectM) {
		this.objectV = objectV;
		this.objectM = objectM;
		
		
		//ADD CONTACT
		addressbook2.View.addButton.addActionListener(new ActionListener() {
			  
				@Override
				public void actionPerformed(ActionEvent arg0) {
			    NewFrame.createFrame1();	
			    
			    addressbook2.NewFrame.submitButton.addActionListener(new ActionListener() {
					  
					@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Model obj = new Model();
				obj.addContact(addressbook2.NewFrame.fNameEntry.getText(), 
						addressbook2.NewFrame.lNameEntry.getText(), 
						addressbook2.NewFrame.groupEntry.getText(), 
						addressbook2.NewFrame.homeAddEntry.getText(),
						addressbook2.NewFrame.compAddEntry.getText(),
						addressbook2.NewFrame.personalMobileEntry.getText(),
						addressbook2.NewFrame.personalLandEntry.getText(), 
						addressbook2.NewFrame.profMobileEntry.getText(), 
						addressbook2.NewFrame.profLandEntry.getText(),
						addressbook2.NewFrame.homePageEntry.getText(),
						addressbook2.NewFrame.emailIdEntry.getText(), 
						addressbook2.NewFrame.photoEntry.getText(),
						addressbook2.NewFrame.dateEntry.getText());		
			}
		    });
		}
		});
		
		
		
		//DELETE CONTACT
		addressbook2.View.deleteButton.addActionListener(new ActionListener() {
			  
			   @Override
	           public void actionPerformed(ActionEvent arg0) {
				
				NewFrame.createFrame2();
				addressbook2.NewFrame.submitButton.addActionListener(new ActionListener() {
					  
					@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Model obj = new Model();
				try {
					obj.deleteContact(addressbook2.NewFrame.fNameEntry.getText(), addressbook2.NewFrame.lNameEntry.getText());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}				
		    });	
	     }
	     });
		
	

		//EDIT CONTACT
		addressbook2.View.editButton.addActionListener(new ActionListener() {
			  
				@Override
		        public void actionPerformed(ActionEvent arg0) {
			    NewFrame.createFrame3();
			    
			    addressbook2.NewFrame.submitButton.addActionListener(new ActionListener() {
					  
					@Override
				public void actionPerformed(ActionEvent arg0) {
					
					Model obj = new Model();
					try {
						ArrayList<String> cont = new ArrayList<String>(); 
						cont = obj.editContact(addressbook2.NewFrame.fNameEntry.getText(), addressbook2.NewFrame.lNameEntry.getText());
						System.out.println(cont);
						NewFrame.createFrame_3(cont);
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			     }
			    });
		}

		});  
		  
		//SORT NAMEWISE
		addressbook2.View.sortName.addActionListener(new ActionListener() {
			  
			    @Override
	            public void actionPerformed(ActionEvent arg0) {
		        NewFrame.createFrame4();
	     }

	     });
		
		
		//SORT GROUPWISE
		addressbook2.View.sortGroup.addActionListener(new ActionListener() {
			  
			@Override
			public void actionPerformed(ActionEvent arg0) {
			NewFrame.createFrame5();
			addressbook2.NewFrame.submitButton.addActionListener(new ActionListener() {
				  
				@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Model obj = new Model();
				try {
					ArrayList<String> grp = new ArrayList<String>(); 
					grp = obj.sortGroup(addressbook2.NewFrame.groupEntry.getText());
					System.out.println(grp);
					NewFrame.createFrame_5(grp);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				}
			});	
			}
		});
		
	

		
		//SORT DATEWISE
		 addressbook2.View.sortDate.addActionListener(new ActionListener() {
			  
				@Override
				public void actionPerformed(ActionEvent arg0) {
			    NewFrame.createFrame6();
		}

		});
		 
		 //DISPLAY CONTACT DETAILS
		 addressbook2.View.displayButton.addActionListener(new ActionListener() {
			  
				@Override
				public void actionPerformed(ActionEvent arg0) {
			    NewFrame.createFrame7();
			    addressbook2.NewFrame.submitButton.addActionListener(new ActionListener() {
					  
					@Override
				public void actionPerformed(ActionEvent arg0) {
					
					Model obj = new Model();
					try {
						ArrayList<String> cont = new ArrayList<String>(); 
						cont = obj.displayContact(addressbook2.NewFrame.fNameEntry.getText(), addressbook2.NewFrame.lNameEntry.getText());
						System.out.println(cont);
						NewFrame.createFrame_7(cont);
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			     }
			    });
				}
		 });
		  
		}// end of constructor
		
} //end of class